<?php

class FavoritesController extends AppController {

	var $helpers = array('Html','Text','Javascript','Ajax','Image');
  
	//add a new favorite
	function add($product_id) {
		//verify identity
		if (!$this->checkSession()) {
			$this->forceLogin();
		}
		$Member = new Member();
		//get member id
		$me = $Member->findByUsername($this->userInfo['username']);
    //pr($me);
		$myId = $me['Member']['member_id'];
		//create favorite
		$favorite = array(
			'Favorite' => array(
				'product_id' => $product_id,
				'member_id' => $myId,
				)
			);
		if ($this->Favorite->findCount("Favorite.member_id = '$myId' AND Favorite.product_id = '$product_id'") == 0) {
			$this->Favorite->save($favorite);
		}
		//bounce to favs/view page
		//$this->redirect('/favorites/view/' . $this->userInfo['username']);
		$this->layout = 'ajax';
		$this->set('id', $product_id);
	}
	
	//remove a favorite
	function delete($product_id) {
		//verify identity
		if (!$this->checkSession()) {
			$this->forceLogin();
		}
		$Member = new Member();
		//get member id
		$me = $Member->findByUsername($this->userInfo['username']);
    //pr($me);
		$myId = $me['Member']['member_id'];
		//remove favorite
		$goner = $this->Favorite->find("Favorite.member_id = '$myId' AND Favorite.product_id = '$product_id'");
		$this->Favorite->del($goner['Favorite']['id']);
		
		$this->layout = 'ajax';
		$this->set('id', $product_id);
	}
	
	//view a member's favorites
	function view($member_name, $layout = 'html') {
		//layout should be html, otherwise ajax/xml view for stripped down list
		//$this->layout($layout);
		//verify identity
		$Member = new Member();
		//lookup member by member name
		$me = $Member->findByUsername($member_name);
		
		//find member's favorites by member id
		//echo "Favorite.member_id = '" . $me['Member']['member_id'] . "'";
		
		$favs = $this->Favorite->findAll('Favorite.member_id = ' . $me['Member']['member_id'],null,null,null,null,0);
		$current_del = Configure::read('Delivery.delivery_id');
		$findProducts = array();
		foreach($favs as $num => $prod) {
			$findProducts[] = 'Product.product_id = ' . $prod['Product']['product_id'];
		}
    	$p = new Product();
    	if (!empty($findProducts)) {
    		$where = (count($findProducts) > 1) ? implode(" OR ", $findProducts) : $findProducts[0];
    		$prods = $p->c_find($where);
    	} else {
    		$prods = array();
    	}
		//set vars for view
		$this->set('favs', $prods); //$favs);
		$this->set('name', $member_name);
		if (isset($this->userInfo)) {
			$this->set('m',$this->userInfo);
		}
	}
	
	//add all my favorites to my basket
	function purchaseAll() {
		//get identity (must be logged in)
		
		//step through and add all products that are available
		
		//setup variables for unavailable/sold out products
		
		//send variables to view
	}
	
	function isFavorite($p_id, $m_id) {
		//checks if this product is a favorite of this member
		
		return ($this->Favorite->findCount("Favorite.member_id = '$m_id' AND Favorite.product_id = '$p_id'") > 0);
	}
	
}

?>
